package com.dbsun.controller.st;

import com.dbsun.base.BaseController;
import com.dbsun.bean.Comments;
import com.dbsun.bean.SmallStudent;
import com.dbsun.common.ServerResponse;
import com.dbsun.entity.system.Page;
import com.dbsun.entity.system.PageData;
import com.dbsun.mapper.CommentconfigMapper;
import com.dbsun.mapper.Comments2Mapper;
import com.dbsun.mapper.SmallStudentMapper;
import com.dbsun.service.st.Small_studentService;
import com.dbsun.service.user.AppUserService;
import io.swagger.annotations.Api;
import com.dbsun.service.st.CommentsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Api(value = "comments", description = "其他评论管理", tags = "其他评论管理")
@Controller
@RequestMapping("/comments")
public class CommentsController extends BaseController {
    @Autowired
    private CommentsService sCommentsService;

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private CommentconfigMapper commentconfigMapper;
    @Autowired
    private Small_studentService smallStudentMapper;

    @RequestMapping(value = "/gosCommentsPage", method = RequestMethod.GET)
    @ApiOperation(value = "跳转到其他评论管理页面")
    public String gosCommentsPage() {
        return "";
    }

    @RequestMapping(value = "/getPagesCommentsList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取其他评论管理列表数据", notes = "分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "showCount", value = "每页记录数", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "currentPage", value = "当前页", required = true, dataType = "String"),
    })
    public JSONObject getPagesCommentsList() {
        PageData pd = this.getPageData();
        Page page = this.getPage();
        page.setPd(pd);
        List<PageData> sCommentsList = null;
        try {
            sCommentsList = sCommentsService.getPageCommentsList(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return viewReturnPageData(page, sCommentsList);
    }

    @RequestMapping(value = "/generateComment", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "自动生成评论")
    @ApiImplicitParams({})
    public JSONObject generateComment() {
        List<PageData> nonecommentUserList = new ArrayList<>();
        Page page = new Page();
        page.setShowCount(10000);
        page.setTotalResult(10000);
        List<PageData> studentList = smallStudentMapper.getPageSmall_studentList(page);
        for (int i = 0; i < studentList.size(); i++) {
            PageData p = new PageData();
            p.put("a_id", studentList.get(i).get("id").toString());
            List<PageData> list = sCommentsService.getCommentsList2(p);
            if (list.size() <= 0) {
                nonecommentUserList.add(studentList.get(i));
                int grade = 0;
                grade += Integer.parseInt(studentList.get(i).get("s_de").toString());
                grade += Integer.parseInt(studentList.get(i).get("s_zhi").toString());
                grade += Integer.parseInt(studentList.get(i).get("s_ti").toString());
                grade += Integer.parseInt(studentList.get(i).get("s_mei").toString());
                grade += Integer.parseInt(studentList.get(i).get("s_nao").toString());
                String c_lv = "";
                c_lv += grade > 20 && grade <= 25 ? "3" : "";
                c_lv += grade > 15 && grade <= 20 ? "2" : "";
                c_lv += grade > 10 && grade <= 15 ? "1" : "";
                c_lv += grade > 0 && grade <= 10 ? "0" : "";
                PageData comments1 = new PageData();
                PageData pageData = new PageData();
                pageData.put("id", c_lv);

                comments1.put("c_content", commentconfigMapper.getCommentConfigById(pageData).get("comment").toString());
                comments1.put("a_id", studentList.get(i).get("id"));
                comments1.put("name", studentList.get(i).get("name"));
                comments1.put("c_title", studentList.get(i).get("name") + " 小朋友");
                comments1.put("c_class", studentList.get(i).get("grade"));
                comments1.put("c_lv", c_lv);
                comments1.put("s_id", "1");
                comments1.put("c_createtime", LocalDateTime.now().toString());
                sCommentsService.addComments(comments1);
            }
        }
        return viewReturnPageData(page, studentList);
    }

    @RequestMapping(value = "/getAllCommentConfig", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部评论模板")
    @ApiImplicitParams({})
    public JSONObject getAllCommentConfig() {
        PageData pageData = new PageData();
        pageData.put("id", "1");
        List<PageData> pageDataList = new ArrayList<>();
        pageDataList.add(commentconfigMapper.getCommentConfigById(pageData));

        pageData = new PageData();
        pageData.put("id", "2");
        pageDataList.add(commentconfigMapper.getCommentConfigById(pageData));

        pageData = new PageData();
        pageData.put("id", "3");
        pageDataList.add(commentconfigMapper.getCommentConfigById(pageData));

        pageData = new PageData();
        pageData.put("id", "0");
        pageDataList.add(commentconfigMapper.getCommentConfigById(pageData));

        return viewReturnPageData(new Page(), pageDataList);

    }

    @RequestMapping(value = "/updateCommentConfig", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部评论模板")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "模板id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "comment", value = "模板内容", required = false, dataType = "String"),
    })
    public JSONObject updateCommentConfig() {
        PageData pd = this.getPageData();
        pd.remove("type");
        System.out.println(pd.toString());
        PageData userPd = this.getUserPd();
        commentconfigMapper.updateCommentConfigById(pd);
        JSONObject json = new JSONObject();// 返回数据必须包含这个格式
        return json;
    }

    @RequestMapping(value = "/addsComments", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "添加其他评论管理", notes = "添加不为空的内容")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "a_id", value = "其他评论管理", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "学生名字", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_title", value = "名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_content", value = "内容", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_class", value = "类目", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_lv", value = "评论等级", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_create_time", value = "时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "s_id", value = "老师id", required = false, dataType = "String"),
    })
    public ServerResponse addsComments() {
        PageData pd = this.getPageData();
        PageData userPd = this.getUserPd();
        return sCommentsService.addComments(this.putUserPd(pd));
    }

    @RequestMapping(value = "/updatesComments", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "更新其他评论管理", notes = "更新不为空的内容")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "xxx", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "a_id", value = "其他评论管理", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "学生名字", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_title", value = "名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_content", value = "内容", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_class", value = "类目", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_lv", value = "评论等级", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_create_time", value = "时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "s_id", value = "老师id", required = false, dataType = "String")
    })
    public ServerResponse updatesCommentsByid() {
        PageData pd = this.getPageData();
        PageData userPd = this.getUserPd();
        return sCommentsService.updateCommentsByid(pd);
    }

    @RequestMapping(value = "/getsCommentsList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取其他评论管理列表数据非分页方法")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "xxx", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "a_id", value = "其他评论管理", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "学生名字", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_title", value = "名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_content", value = "内容", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_class", value = "类目", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_lv", value = "评论等级", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_create_time", value = "时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "s_id", value = "老师id", required = false, dataType = "String")
    })
    public ServerResponse getsCommentsList() {
        PageData pd = this.getPageData();
        PageData userPd = this.getUserPd();
        return sCommentsService.getCommentsList(pd);
    }


    @RequestMapping(value = "/getsCommentsByColumn", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取其他评论管理单条数据(通过任意列)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "xxx", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "a_id", value = "其他评论管理", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "学生名字", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_title", value = "名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_content", value = "内容", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_class", value = "类目", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_lv", value = "评论等级", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_create_time", value = "时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "s_id", value = "老师id", required = false, dataType = "String")
    })
    public ServerResponse getsCommentsByColumn() {
        PageData pd = this.getPageData();
        PageData userPd = this.getUserPd();
        return sCommentsService.getCommentsByColumn(pd);
    }

    @RequestMapping(value = "/deletesComments", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除其他评论管理", notes = "删除不为空的内容")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "xxx", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "a_id", value = "其他评论管理", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "学生名字", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_title", value = "名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_content", value = "内容", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_class", value = "类目", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_lv", value = "评论等级", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "c_create_time", value = "时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "s_id", value = "老师id", required = false, dataType = "String")
    })
    public ServerResponse deletesCommentsByColumn() {
        PageData pd = this.getPageData();
        return sCommentsService.deleteCommentsByColumn(pd);
    }
}
